package com.yuchen.advsdk.util;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ServiceInfo;
import android.util.Log;

public class ManifestMetaDataUtil {
    static String TAG = "MetaDataUtil";

    /**
     *
     * @param context
     * @param metaDataName
     * @return
     *
     * <meta-data    android:name="plunInstallByChannel"     android:value="WWStore" />
     * 调用例子：String  metaDataValue =ManifestMetaDataUtil.getMetaDataInApplicaiton(this,"plunInstallByChannel");
     */
    static public String getMetaDataInApplicaiton(Context context, String metaDataName){
        ApplicationInfo appInfo = null;
        try {
            appInfo = context.getPackageManager()
                    .getApplicationInfo(context.getPackageName(),
                            PackageManager.GET_META_DATA);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return null;
        }
        String msg=appInfo.metaData.getString(metaDataName);
        Log.d(TAG, " metaData Value == " + msg );
        return msg;
    }

    /**
     *
     * @param activity
     * @param metaDataName
     * @return
     *
     * <meta-data android:name="czgActivity" android:value="hello my activity"></meta-data>
     * String  metaDataValue =ManifestMetaDataUtil.getMetaDataInApplicaiton(this,"plunInstallByChannel");
     */
    static public String getMetaDataInActivity(Activity activity, String metaDataName){


        ActivityInfo info= null;
        try {
            info = activity.getPackageManager()
                    .getActivityInfo(activity.getComponentName(),
                            PackageManager.GET_META_DATA);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return null;
        }
        String msg =info.metaData.getString(metaDataName);

        Log.d(TAG, " metaData Value in Activity == " + msg );
        return msg;
    }

    static public String getMetaDataInService(Context context, Class<?> serverClazz, String metaDataName){


        ComponentName cn=new ComponentName(context, serverClazz);
        ServiceInfo info= null;
        try {
            info = context.getPackageManager()
                    .getServiceInfo(cn, PackageManager.GET_META_DATA);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return null;
        }
        String msg=info.metaData.getString(metaDataName);

        Log.d(TAG, " metaData Value in Service == " + msg );
        return msg;
    }


    static public String getMetaDataInReceiver(Context context, Class<?> receiverClazz, String metaDataName){

        ComponentName cn=new ComponentName(context, receiverClazz);
        ActivityInfo info= null;
        try {
            info = context.getPackageManager()
                    .getReceiverInfo(cn, PackageManager.GET_META_DATA);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return null;
        }
        String msg=info.metaData.getString(metaDataName);

        Log.d(TAG, " metaData Value in BroadcastReceiver == " + msg );
        return msg;
    }


}
